package com.ruoyi.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.domain.AdvPlatinfos;
import com.ruoyi.service.BaiduService;
import com.ruoyi.service.TxyService;
import com.ruoyi.utils.MyFileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.AdvBroadcastMapper;
import com.ruoyi.domain.AdvBroadcast;
import com.ruoyi.service.IAdvBroadcastService;

/**
 * Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@Slf4j
@Service
public class AdvBroadcastServiceImpl implements IAdvBroadcastService
{
    @Autowired
    private AdvBroadcastMapper advBroadcastMapper;

    @Autowired
    private TxyService txyService;

    /**
     * 查询
     *
     * @param advBroadcastId 主键
     * @return
     */
    @Override
    public AdvBroadcast selectAdvBroadcastByAdvBroadcastId(Long advBroadcastId)
    {
        return advBroadcastMapper.selectAdvBroadcastByAdvBroadcastId(advBroadcastId);
    }

    /**
     * 查询列表
     *
     * @param advBroadcast
     * @return
     */
    @Override
    public List<AdvBroadcast> selectAdvBroadcastList(AdvBroadcast advBroadcast)
    {
        return advBroadcastMapper.selectAdvBroadcastList(advBroadcast);
    }

    /**
     * 新增
     *
     * @param advBroadcast
     * @return 结果
     */
    @Override
    public int insertAdvBroadcast(AdvBroadcast advBroadcast)
    {
        if(advBroadcast.getBroadstatus() == null) {
            advBroadcast.setBroadstatus(2L);
        }
        advBroadcast.setIsAudit(0L);
        /*
            需要设置直播的连接
         */
        String pushAddress = txyService.getPushAddress("rtmp://", advBroadcast.getAdvMembersId() + "");
        String playAddress = txyService.getPlayAddress("rtmp://", advBroadcast.getAdvMembersId() + "");
        advBroadcast.setBroadUrl(playAddress);
        advBroadcast.setPushUrl(pushAddress);
        return advBroadcastMapper.insertAdvBroadcast(advBroadcast);
    }

    /**
     * 修改
     *
     * @param advBroadcast
     * @return 结果
     */
    @Override
    public int updateAdvBroadcast(AdvBroadcast advBroadcast)
    {
        return advBroadcastMapper.updateAdvBroadcast(advBroadcast);
    }

    /**
     * 批量删除
     *
     * @param advBroadcastIds 需要删除的主键
     * @return 结果
     */
    @Override
    public int deleteAdvBroadcastByAdvBroadcastIds(Long[] advBroadcastIds)
    {
        List<AdvBroadcast> advBroadcastList = new ArrayList<AdvBroadcast>();
        for (Long advBroadcastId : advBroadcastIds) {
            AdvBroadcast broadcast = advBroadcastMapper.selectAdvBroadcastByAdvBroadcastId(advBroadcastId);
            if(!Objects.isNull(broadcast)) {
                advBroadcastList.add(broadcast);
            }
        }
        int i = 0;
        try {
            advBroadcastMapper.deleteReservbroadcast(advBroadcastIds);
            advBroadcastMapper.deleteDeservbroadcast(advBroadcastIds);
            i = advBroadcastMapper.deleteAdvBroadcastByAdvBroadcastIds(advBroadcastIds);
        }catch (DataIntegrityViolationException e){
            log.error("直播删除失败{}",e);
            throw new RuntimeException("请先删除关联数据");
        }
        if(i > 0) {
            for (AdvBroadcast broadcast : advBroadcastList) {
                MyFileUtils.deleteFile(broadcast.getImage());
            }
        }
        return i;
    }

    /**
     * 删除信息
     *
     * @param advBroadcastId 主键
     * @return 结果
     */
    @Override
    public int deleteAdvBroadcastByAdvBroadcastId(Long advBroadcastId)
    {
        try {
            return advBroadcastMapper.deleteAdvBroadcastByAdvBroadcastId(advBroadcastId);
        }catch (DataIntegrityViolationException e){
            log.error("直播删除失败{}",e);
            throw new RuntimeException("请先删除关联数据");
        }

    }

    @Override
    public boolean startLive(Long advMembersId) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(date);
        System.out.println(currentTime);

        int i = advBroadcastMapper.startLive(advMembersId, currentTime);
        if(i == 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean stopLive(Long advMembersId) {
        int i = advBroadcastMapper.stopLive(advMembersId);
        if(i == 0) {
            return false;
        }
        return true;
    }
    /**
     * 查询列表 - app
     *
     * @param advBroadcast
     * @return
     */
    @Override
    public List<AdvBroadcast> selectAdvBroadcastListApp(AdvBroadcast advBroadcast)
    {
        return advBroadcastMapper.selectAdvBroadcastListApp(advBroadcast);
    }

    @Override
    public List<AdvBroadcast> getBroadByDevicesno(String devicesno, String advMembersId) {
        List<AdvBroadcast> broadcasts =
                advBroadcastMapper.getBroadByDevicesno(devicesno, advMembersId);
        return broadcasts;
    }

    @Override
    public AdvBroadcast status(String advMembersId) {
        AdvBroadcast statusCode = advBroadcastMapper.status(advMembersId);
        return statusCode;
    }

}
